package com.woniuxy.usercoachassemble.commons;

import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
 * ClassName AESHelper
 * @description: TODO(加密解密工具类)
 * @author: Fy浪迹、
 * @date: 2020/7/9 15:53
 */

public class AESHelper {
	private static final String KEY="523abc23143aefef";
	//加密方法
	public static String encode(String content) throws Exception {
		//创建秘钥对象
		SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "AES");
		//获取密码类
		Cipher cipher = Cipher.getInstance("AES");
		//初始化密码类
		cipher.init(Cipher.ENCRYPT_MODE, keySpec);
		//加密得到密文
		byte[] encodeData = cipher.doFinal(content.getBytes());
		//将加密后的字节数据转为字符串
		return Base64.getEncoder().encodeToString(encodeData);
	}
	//解密方法
	public static String decode(String content) throws Exception{
		//创建秘钥对象
		SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "AES");
		//获取密码类
		Cipher cipher = Cipher.getInstance("AES");
		//初始化密码类
		cipher.init(Cipher.DECRYPT_MODE, keySpec);
		//将密文转为字节数组
		byte[] data = Base64.getDecoder().decode(content);
		byte[] b = cipher.doFinal(data);
		return new String(b);
	}
}
